package com.rbc.rlsplips.saop.handlers;



import java.io.ByteArrayOutputStream;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPFactory;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPHeaderElement;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;



public class Handler implements SOAPHandler<SOAPMessageContext> {

	@Override
	public void close(MessageContext arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean handleFault(SOAPMessageContext arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean handleMessage(SOAPMessageContext ctxt) {

		SOAPMessage msg = ctxt.getMessage();

		boolean request = ((Boolean) ctxt.get(SOAPMessageContext.MESSAGE_OUTBOUND_PROPERTY)).booleanValue();

		try {

			SOAPPart sp = msg.getSOAPPart();                     //edit Envelope                   
			SOAPEnvelope env = sp.getEnvelope();   
			//SOAPHeader hd = env.getHeader();
			//SOAPBody bd = msg.getSOAPBody();

			//ObjectFactory objFactory = new ObjectFactory();

			//ContexteSession cs = objFactory.createContexteSession();

			//SOAPElement se = (SOAPElement)cs;
			//hd.addChildElement(se);

			//env.addNamespaceDeclaration("xsd", "http://www.w3.org/2001/XMLSchema");                   
			//env.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance");                   
			//env.addNamespaceDeclaration("soap", "http://schemas.xmlsoap.org/soap/envelope");   

			//env.removeAttributeNS("S", "S");

			//bd.removeNamespaceDeclaration("ns2");


			//env.addNamespaceDeclaration("ns2", "http://www.mrq.gouv.qc.ca/T1A/04111900/Communs");  
			//env.createQName("ContexteSession", "ns2");
			//env.addNamespaceDeclaration("ns", "http://www.mrq.gouv.qc.ca/T1A/04111900/Annuaire"); 

			QName qname=new QName("http://www.mrq.gouv.qc.ca/T1A/04111900/Communs","ContexteSession");

			//hd.removeNamespaceDeclaration("ns2");
			// boolean st = hd.removeAttribute(qname);

			//hd.addNamespaceDeclaration("ns2",  "http://www.mrq.gouv.qc.ca/T1A/04111900/Communs");

			//boolean an = hd.removeNamespaceDeclaration("ns2");


			SOAPFactory sf = SOAPFactory.newInstance();
			SOAPElement se = sf.createElement(qname);

			SOAPHeader sphd = env.getHeader();
			if (sphd == null){
				sphd = env.addHeader();
			}				
			SOAPHeaderElement shdelem = sphd.addHeaderElement(qname);

			shdelem.setAttribute("NoTed", "000222");
			shdelem.setAttribute("NoUsager", "1006006988");
			shdelem.setAttribute("CodeService", "SW1000");
			shdelem.setAttribute("NoVersionService", "4.11.19.0");
			shdelem.setAttribute("IdPartenaire", "7CD4AB73-BA97-4c9f-A3F2-055A28D5C6A0");
			shdelem.setAttribute("IdLogiciel", "70A9A439-5107-4586-A71F-D3D18476FE29");
			shdelem.setAttribute("NoSerieLogiciel", "2011");



			//Iterator it =hd.getVisibleNamespacePrefixes();
			//while(it.hasNext()){
			//	   System.out.println(it.next());
			// }

			//System.out.println(an);

			msg.saveChanges();

			/*
			   	ObjectFactory objFactory = new ObjectFactory();

			    ContexteSession cs = objFactory.createContexteSession();

			    cs.setNoTed("000222");
				cs.setNoUsager("1006006988");
				cs.setCodeService("SW1000");
				cs.setNoVersionService("4.11.19.0");
				cs.setIdPartenaire("7CD4AB73-BA97-4c9f-A3F2-055A28D5C6A0");
				cs.setIdLogiciel("70A9A439-5107-4586-A71F-D3D18476FE29");

				cs.setNoSerieLogiciel("2011");

				hd.addChildElement(cs);
			 */

			if (request) { // This is a request message.
				// Write the message to the output stream 
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				msg.writeTo(baos);
				System.out.println(baos.toString("utf-8"));

				// show included values
				String values = msg.getSOAPBody().getTextContent();
				System.out.println("Included values:" + values);

			}
			else { // This is the response message 
				//System.out.println(msg);
			}
		}catch (Exception e) {e.printStackTrace(); }




		return true;
	}

	@Override
	public Set<QName> getHeaders() {
		// TODO Auto-generated method stub
		return null;
	}

}

